#!/usr/bin/python 
#coding=utf-8
import MySQLdb as mdb

class add_video_to_group:
	def __init__(self):
		return
	def set_spider_video_id(self,id):
		self.spider_video_id = id;

	def set_video_id(self,id):
		self.video_id = id;

	def get_group(self):
		self.conn 	= mdb.connect('localhost','root','mgw198100','video_spider');
		self.cursor	= self.conn.cursor(mdb.cursors.DictCursor);
		cmd 		="SELECT product_group_id FROM MOVIE_QUEUE, SPIDER_PRODUCT WHERE MOVIE_QUEUE.id =  '"+\
					self.spider_video_id+"' AND MOVIE_QUEUE.video_product_id = SPIDER_PRODUCT.id";
		#print cmd;
		self.cursor.execute(cmd);
		rows            = self.cursor.fetchall();
		self.cursor.close();
		self.conn.close();

		if len(rows) == 0:
			return False;
		else:
			self.group_id=str(rows[0]['product_group_id'])
			return True;
		
	def add_video(self):
		self.conn 	= mdb.connect('localhost','root','mgw198100','sns');
		self.cursor	= self.conn.cursor(mdb.cursors.DictCursor);
		self.cursor.execute("SET NAMES 'utf8'");
		self.sql_cmd  = "INSERT INTO bx_groups_videos(entry_id,media_id) VALUES(%s,%s)"%(self.group_id,self.video_id)

		self.cursor.execute(self.sql_cmd);
		self.cursor.close();
		self.conn.close();
	def group_status(self,status):
		self.conn 	= mdb.connect('localhost','root','mgw198100','video_spider');
		self.cursor	= self.conn.cursor(mdb.cursors.DictCursor);
		cmd		= "UPDATE MOVIE_QUEUE SET video_status='%s' WHERE id='%s'"%(status,self.spider_video_id)
		#print cmd;
		self.cursor.execute(cmd);
		self.cursor.close();
		self.conn.close();
		
	def group_suc(self):
		self.group_status("group_suc")

	def group_fail(self):
		self.group_status("group_fail")
		

if __name__ == "__main__":
	test = add_video_to_group()
	test.set_spider_video_id("106");
	print test.get_group();
	test.set_video_id("65");
	test.add_video();
	test.group_suc();



